<?php

class Admin_UsersController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $adminValidation = new Validations_User();
        $valid = $adminValidation->hasidentity();
        if ($valid) {
            if ($adminValidation->getUserTypeId() != 1) {
                $this->view->message = "You are not admin.";
                Zend_Auth::getInstance()->clearIdentity();
                $this->_redirect('/admin/login');
            }
        } else {
            $this->_redirect('/admin/login');
        }
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction() {
        $db = new Users_Model_Users();
        $this->view->allUsers = $db->getAllUsers();
    }

    public function userblockAction() {
        $id = $this->getinputidAction(); //user id
        $blocked = true;
        $this->userbannAction($id, $blocked);
    }

    public function userunblockAction() {
        $id = $this->getinputidAction(); //user id
        $blocked = false;
        $this->userbannAction($id, $blocked);
    }

    public function userbannAction($id, $param) {
        $db = new Users_Model_Users();
        $data = array(
            'blocked' => $param
        );
        $db->updateUser($data, $id);
        $this->_redirect('/admin/manage-users');
    }

    /**
     * Get User ID.
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; //user id   
        return $id;
    }

    public function deleteuserAction() {
        echo "User ID = " . $id = $this->getinputidAction();
        $db = new Users_Model_Users();
        $propertyDb = new Ads_Model_Rentdetails();
        $rents = $propertyDb->getRentDetailsByUserId($id);
        foreach ($rents as $value) {
            $propertyId = $value['id'];
            $propertyDb->deleteProperty($propertyId);
        }
        $db->deleteUser($id);
        $this->_redirect('/admin/manage-users');
    }

}

